package test00;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Scanner;

/**
 * @Author lzp
 * @Date 2024-03-26
 */
public class test06 {
    public static void main(String[] args) throws Exception {

        //获取 Scanner 类的 Class 对象
        Class<Scanner> clazz = Scanner.class;
        //获取 String 类的 Class 对象
        Class<String> strClass = String.class;

        //实例化对象  -  getConstructor获取构造函数   -  newInstance 通过反射动态创建一个类的实例
        Scanner scanner = clazz.getConstructor(InputStream.class).newInstance(System.in);

        System.out.println("请输入邮箱");
        //获取方法 - 公开     next()接收输入的字符串
        Method studyMethod = clazz.getMethod("next");
        //subSequence() 截取一个下标到第二个下标前的字符
        Method subSequenceMethod = strClass.getMethod("subSequence", int.class, int.class);
        //indexOf() 获取某个字符的下标
        Method indexOfMethod = strClass.getMethod("indexOf", String.class);


        String email = (String) studyMethod.invoke(scanner);
        int index = (int) indexOfMethod.invoke(email, "@");
        Object user = subSequenceMethod.invoke(email,0, index);
        System.out.println("您的用户名是:"+user);

    }
}
